class DynamicMap extends Object {
  final Map<String, dynamic> _map;

  DynamicMap(this._map);

  @override
  dynamic noSuchMethod(Invocation invocation) {
    final key = _symbolToString(invocation.memberName);

    if (_map.containsKey(key)) {
      final value = _map[key];

      // 嵌套 Map 自动封装
      if (value is Map<String, dynamic>) {
        return DynamicMap(value);
      }

      // 如果是 List<Map> 也自动封装每个元素
      if (value is List && value.isNotEmpty && value.first is Map<String, dynamic>) {
        return value.map((e) => DynamicMap(e)).toList();
      }

      return value; // 普通值直接返回
    }

    return super.noSuchMethod(invocation);
  }

  String _symbolToString(Symbol symbol) {
    return symbol.toString().replaceAll('Symbol("', '').replaceAll('")', '');
  }

  @override
  String toString() {
    return _map.toString();
  }
}